<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;

/**
 * 个人帐号
 *
 * @author Xmh
 */
class ClcwUserController extends BaseController
{
    private $clcwUserLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->clcwUserLogic = D('ClcwUser', 'Logic');
        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }

    public function clcwUserList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['uid'] = I('get.uid/d', 0);
        $params['user_name'] = I('get.user_name', '');
        $params['real_name'] = I('get.real_name', '');
        $params['phone'] = I('get.phone', '');
        $params['level'] = I('get.level/d', 0);
        $params['is_expert'] = I('get.is_expert/d', 0);
        $params['is_recommend_expert'] = I('is_recommend_expert/d', 0);
        $params['status'] = I('get.status', -1);
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');

        $result = $this->clcwUserLogic->getClcwUserList($page, $size, $params);

        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }


    /**
     * 设置牛人
     */
    public function setExpert()
    {
        $userId = I('get.uid/d', 0);
        if ($userId < 1) {
            $this->redirect('ClcwUser/index', '', 1, '请求出错了');
        }
        $user_info = $this->clcwUserLogic->getClcwUserInfo($userId);
        if ($user_info['is_expert'] == 1) {
            $this->redirect('ClcwUser/index', '', 1, '请求出错了');
        }
        $this->assign('user_id', $userId);
        $this->display();
    }

    /**
     * 设置牛人
     */
    public function setUserExpert()
    {
        $userId = I('post.uid/d', 0);
        $brand = I('post.brand', '', 'trim');
        $intro = I('post.intro', '', 'trim');
        if (empty($brand)) {
            $this->helpJsonResult('400', '擅长品牌不能为空');
        }
        if (empty($intro)) {
            $this->helpJsonResult('400', '牛人介绍不能为空');
        }
        $data['user_id'] = $userId;
        $data['expert_brand'] = $brand;
        $data['user_intro'] = $intro;
        $data['is_expert'] = 1;
        $data['change_expert_time'] = time();
        $res = $this->clcwUserLogic->updateUserInfo($data);
        if (!$res) {
            $this->helpJsonResult('400', '设置牛人失败');
        }
        $this->helpJsonResult('200', '设置牛人成功');
    }

    /**
     * 取消牛人
     */
    public function cancelUserExpert()
    {
        $userId = I('post.user_id/d', 0);
        if ($userId < 1) {
            $this->helpJsonResult('400', '牛人不存在');
        }
        $data['user_id'] = $userId;
        $data['is_expert'] = 0;
        $data['expert_brand'] = '';
        $data['user_intro'] = '';
        $res = $this->clcwUserLogic->updateUserInfo($data);
        if (!$res) {
            $this->helpJsonResult('400', '取消牛人失败');
        }
        $this->helpJsonResult('200', '取消牛人成功');
    }


    /**
     * 设置用户状态(1、正常 2、禁用)
     */
    public function setUserStatus()
    {
        try {
            $userId = I('post.user_id/d', 0);
            $userStatus = I('post.user_status/d', 0);
            $user = [];
            $user['account_id'] = $userId;
            $user['emp_id'] = $this->user_info['emp_id'];
            $user['emp_ip'] = get_client_ip();
            $res = true;
            if ($userStatus == 2) {
                $res = $this->clcwUserLogic->disableClcwUser($user, $userId);
            } else if ($userStatus == 1) {
                $res = $this->clcwUserLogic->enableClcwUser($user, $userId);
            }
            if (!$res) {
                $this->helpJsonResult('500', '操作失败');
            }
            $this->helpJsonResult('200', '操作成功');
        } catch (AuctionException $e) {
            $this->helpJsonResult($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 设置推荐牛人(0、取消推荐 1、推荐 )
     */
    public function setUserTop()
    {
        $userId = I('post.user_id/d', 0);
        $isTop = I('post.is_top/d', 0);
        if ($isTop == 1) {
            $msg = '推荐';
        } else {
            $msg = '取消推荐';
        }
        if (empty($userId)) {
            $this->helpJsonResult('400', '用户不存在');
        }
        $data['user_id'] = $userId;
        $data['is_top'] = $isTop;
        $res = $this->clcwUserLogic->updateUserInfo($data);
        if (!$res) {
            $this->helpJsonResult('400', $msg . '失败');
        }
        $this->helpJsonResult('200', $msg . '成功');
    }

    /**
     * 编辑用户详情
     */
    public function clcwUserEdit()
    {
        $userId = I('get.uid/d', 0);
        if ($userId < 1) {
            $this->redirect('ClcwUser/index', '', 1, '请求出错了');
        }
        $userInfo = $this->clcwUserLogic->getClcwUserInfo($userId);
        $myCarList = $this->clcwUserLogic->getClcwUserCar($userId);
        $questionCategoryList = D('Question', 'Logic')->getQuestionCategoryList(true);
        $this->assign('clcw_user_info', $userInfo);
        $this->assign('my_car_list', $myCarList);
        $this->assign('category_list', $questionCategoryList);
        $this->display();
    }

    /**
     * 个人账号编辑更新
     */
    public function clcwUserEditData()
    {
        $userId = I('post.uid/d', 0);
        $isExpert = I('post.is_expert/d', 0);
        if ($isExpert == 1) {
            $userInfo = $this->clcwUserLogic->getUserInfoByUid($userId);
            if ($userInfo['is_expert'] == 0) {
                $data['change_expert_time'] = NOW_TIME;
            }
            $isTop = I('post.is_recommend_expert/d', 0);
            $data['is_top'] = $isTop == 1 ? 1 : 0;
            $expertBrand = I('post.good_brand', '', 'trim');
            if (empty($expertBrand)) {
                $this->helpJsonResult('400', '请选择牛人擅长品牌');
            }
            $expertBrandData = $this->uniqueArr(explode(',', $expertBrand));
            $data['expert_brand'] = $expertBrandData;
            $expertGoodCategory = I('post.good_category', '', 'trim');
            if (empty($expertGoodCategory)) {
                $this->helpJsonResult('400', '请选择牛人擅长类型');
            }
            $data['expert_category'] = $expertGoodCategory;
        }
        $data['user_id'] = $userId;
        $data['is_expert'] = $isExpert;
        $data['user_intro'] = I('post.expert_intro', '', 'trim');
        if (empty($data['user_intro'])) {
            $this->helpJsonResult('400', '请填写牛人介绍');
        }
        $res = $this->clcwUserLogic->updateClcwUserEditInfo($data);
        if ($res) {
            $this->helpJsonResult('200', '保存成功');
        } else {
            $this->helpJsonResult('400', '保存失败');
        }
    }

    /**
     * 数组去重
     *
     * @param array $arr
     */
    private function uniqueArr($arr = [])
    {
        if (empty($arr)) {
            return;
        }
        $arrUnique = array_unique($arr);
        return join(',', $arrUnique);
    }


    /**
     * 查看用户详情
     */
    public function clcwUserInfo()
    {
        $userId = I('get.uid/d', 0);
        if ($userId < 1) {
            $this->redirect('ClcwUser/index', '', 1, '请求出错了');
        }
        $userInfo = $this->clcwUserLogic->getClcwUserInfo($userId);
        $myCarList = $this->clcwUserLogic->getClcwUserCar($userId);
        $this->assign('clcw_user_info', $userInfo);
        $this->assign('my_car_list', $myCarList);
        $this->display();
    }

    /**
     * 获取用户成长值
     */
    public function userGrowthList()
    {
        $p = I('request.p', 1);
        $size = 10;
        $params = [];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $userId = I('get.uid/d', 0);
        $params['uid'] = $userId;
        $result = $this->clcwUserLogic->getGrowthList($p, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('p', $p);
        $this->assign('list', $result['list']);
        $this->assign('growth_list', $result['list']);
        $this->display();
    }

    /**
     * 获取用户积分
     */
    public function userScoreList()
    {
        $p = I('request.p', 1);
        $size = 10;
        $params = [];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $userId = I('get.uid/d', 0);
        $params['uid'] = $userId;
        $result = $this->clcwUserLogic->getScoreList($p, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('p', $p);
        $this->assign('list', $result['list']);
        $this->assign('score_list', $result['list']);
        $this->display();
    }

    /**
     * 获取用户积分
     */
    public function userWalletList()
    {
        $p = I('request.p', 1);
        $size = 10;
        $params = [];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $userId = I('get.uid/d', 0);
        $params['uid'] = $userId;
        $result = $this->clcwUserLogic->getWalletList($p, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('p', $p);
        $this->assign('list', $result['list']);
        $this->assign('wallet_list', $result['list']);
        $this->display();
    }

    /**
     * 检测牛人用户
     */
    public function checkClcwExpertUser()
    {
        $userId = I('post.userId/d', 0);
        if ($userId < 1) {
            $this->helpJsonResult(400, '抱歉，查询用户不存在');
        }
        $userInfo = $this->clcwUserLogic->getExpertUserInfoByUid($userId);
        if (empty($userInfo)) {
            $this->helpJsonResult(400, '抱歉，查询用户不存在');
        }
        $userInfo['nick_name'] = $userInfo['nick_name'] ? $userInfo['nick_name'] : showHideMobile($userInfo['mobile']);
        echo $this->helpJsonResult(200, '', $userInfo);
    }
}